package chamans.away
{
	import away3d.containers.ObjectContainer3D;
	import away3d.materials.BitmapMaterial;
	import away3d.entities.Mesh;
	import flash.geom.*;
	import com.bulletflash.dynamics.*;
	
	public class fatc
	{
		private var BASE:Object;
		private var intern:Mesh;
		private var end:Function;
		private var Pos:Vector3D;
		private var mat:BitmapMaterial;
		private var m_liste:Vector.<Mesh>;
		private var r_liste:Vector.<btRigidBody>;
		private var intern_liste:Vector.<Mesh>;
		private var posX:Array=[ new Vector3D(200, 70, -800), new Vector3D(-200, 70, -800),  new Vector3D(-70, 70, -800),  new Vector3D(70, 70, -800) ]
			
		public function fatc(base:Object, END:Function=null, P:Vector3D=null)
		{
			BASE=base; end=END || new Function();
			BASE.LOAD.IMGLIST("assets/demobullet/fatc/", ['fatc_d', "fatc_s", "fatc_local" ], initMaterials , ".jpg")	
		}
		private function initMaterials( A:Array  ) : void
		{
			mat = new BitmapMaterial(A[0]);	
			mat.ambientColor = BASE.FogColor;
			mat.specularMap = A[1];
			mat.normalMap = A[2];
			mat.lights = BASE.LIGHT;
			mat.specular = 2;
			mat.ambient=.1;
			mat.gloss = 60;
			mat.name="fatc";
			if(BASE.reflect)BASE.F_reflect(mat, .5)
			if(BASE.fog) BASE.F_Fog(mat)
			
			BASE.MatListe.push(mat);
			m_liste = new Vector.<Mesh>();
			intern_liste = new Vector.<Mesh>();
			r_liste = new Vector.<btRigidBody>();
			BASE.OBJ3D('assets/demobullet/fatc/fatc.obj', Define);
		}
		private function Define(obj:ObjectContainer3D) : void
		{
			var i:int
			for (i = 0; i < obj.numChildren; ++i) {
				intern_liste.push(obj.getChildAt(i) as Mesh);
			}
			BASE.Pause(end);end=null;
		}
		public function clone(p:Vector3D=null) : void
		{
			var i:int;
			for (i = 0; i < 4; ++i) {
			var mesh:Mesh = intern_liste[i].clone() as Mesh;
			if(p) Pos=posX[i].add(p); else Pos=posX[i].add(RanPos());
			if(BASE.phyOn) Physics(mesh, i, Pos); else mesh.position=Pos;
			mesh.material=mat;
			BASE.view.scene.addChild(mesh);
			m_liste.push(mesh);
		}}
		public function Delete() : void
		{
			if(BASE.phyOn){ while(r_liste.length){
				BASE.physics.world.removeRigidBody(r_liste[r_liste.length]);
				r_liste.pop();
			}}
			while(m_liste.length){
				BASE.view.scene.removeChild(m_liste[m_liste.length]);
				m_liste.pop();
			}
		}
		private function RanPos() : Vector3D {
			return new Vector3D(BASE.TOOL.Ran(1000, -1000), 70, BASE.TOOL.Ran(1000));
		}
		private function Physics(mesh:Mesh, n:int=0, p:Vector3D=null) : void
		{
			var name:String;
			var phyData:Vector3D=new Vector3D(3, .3, .3);
			if(n==0)name="f"
			if(n==1)name="a"
			if(n==2)name="t"
			if(n==3)name="c"
			mesh=BASE.physics.Make(name, m_liste, r_liste, p, new Vector3D(), phyData, mesh, false);
		}		
	}}
